/**
 * Copyright &copy; 2012-2013 <a href="http://www.transnal.com">TransnalCms</a> All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.quanquan.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 *
 * @author Transnal
 * @version 2013-3-15
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
        "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"};

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd）
     */
    public static String getDate() {
        return getDate("yyyy-MM-dd");
    }

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String getDate(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    /**
     * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Date date, Object... pattern) {
        String formatDate = null;
        if (pattern != null && pattern.length > 0) {
            formatDate = DateFormatUtils.format(date, pattern[0].toString());
        } else {
            formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        }
        return formatDate;
    }

    /**
     * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String formatDateTime(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 得到日期时间字符串，转换格式（MM-dd）
     */
    public static String formatDateMonthDay(Date date) {
        return formatDate(date, "MM-dd");
    }

    /**
     * 得到日期时间字符串，转换格式（dd）
     */
    public static String formatDateOnlyDay(Date date) {
        return formatDate(date, "dd");
    }

    /**
     * 得到日期时间字符串，转换格式（dd）
     */
    public static String formatDateOnlyHour(Date date) {
        return formatDate(date, "HH");
    }

    /**
     * 得到日期时间字符串，转换格式（HH:mm）
     */
    public static String formatDateHourAndMinute(Date date) {
        return formatDate(date, "HH:mm");
    }

    /**
     * 得到日期时间字符串，转换格式（yyyy）
     */
    public static String formatDateOnlyYear(Date date) {
        return formatDate(date, "yyyy");
    }

    /**
     * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String formatDateDay(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm");
    }

    /**
     * 得到当前时间字符串 格式（HH:mm:ss）
     */
    public static String getTime() {
        return formatDate(new Date(), "HH:mm:ss");
    }

    /**
     * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String getDateTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 得到当前年份字符串 格式（yyyy）
     */
    public static String getYear() {
        return formatDate(new Date(), "yyyy");
    }

    /**
     * 得到当前月份字符串 格式（MM）
     */
    public static String getMonth() {
        return formatDate(new Date(), "MM");
    }

    /**
     * 得到当天字符串 格式（dd）
     */
    public static String getDay() {
        return formatDate(new Date(), "dd");
    }

    /**
     * 得到当前星期字符串 格式（E）星期几
     */
    public static String getWeek() {
        return formatDate(new Date(), "E");
    }

    /**
     * 日期型字符串转化为日期 格式
     * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" }
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取过去的天数
     *
     * @param date
     * @return
     */
    public static long pastDays(Date date) {
        long t = new Date().getTime() - date.getTime();
        return t / (24 * 60 * 60 * 1000);
    }


    public static Date getDateStart(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date getDateEnd(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 23:59:59");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取过去和以后的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * 把2014-01-05 格式化成20140105
     */
    public static String getFormateDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
        Date da = null;
        try {
            da = sdf.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sd.format(da);
    }

    /**
     * 把2014-01-05 格式化成201401
     */
    public static String getFormateDate3(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM");
        Date da = null;
        try {
            da = sdf.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sd.format(da);
    }

    /**
     * 把2014-01 格式化成201401
     */
    public static String getFormateDate4(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMM");
        Date da = null;
        try {
            da = sdf.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sd.format(da);
    }

    /**
     * 把20140105 格式化成201401
     */
    public static String getFormateDate2(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMM");
        Date da = null;
        try {
            da = sdf.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sd.format(da);
    }

    /**
     * 把20140105 格式化成2014-01-05
     */
    public static String getStringToDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        Date da = null;
        try {
            da = sdf.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sd.format(da);
    }

    /**
     * 统计报表 将时间转换long类型
     *
     * @param date
     * @return
     */
    public static Long getLongByDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date da = null;
        try {
            da = sdf.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Calendar ca = Calendar.getInstance();
        ca.setTime(da);
        ca.set(Calendar.DATE, ca.get(Calendar.DATE) + 1);
        return ca.getTimeInMillis();
    }

    /**
     * 得到日期时间字符串，转换格式（yyyy-MM-dd）
     */
    public static Date formatToDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date da = new Date();
        try {
            da = sdf.parse(date);
        } catch (ParseException e) {
            return null;
        }
        return da;
    }

    /**
     * 得到日期时间字符串，转换格式（yyyy-MM）
     */
    public static Date formatToDate2(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date da = new Date();
        try {
            da = sdf.parse(date);
        } catch (ParseException e) {
            return null;
        }
        return da;
    }

    public static String getWeekBetween(int n) {

        Calendar cal = Calendar.getInstance();
        //n为推迟的周数，1本周，-1向前推迟一周，2下周，依次类推
        String monday;
        cal.add(Calendar.DATE, n * 7);
        //想周几，这里就传几Calendar.MONDAY（TUESDAY...）
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return monday;
    }

    public static String getAfterminute(int minute) {

        Calendar cal = Calendar.getInstance();
        String monday;
        cal.add(Calendar.MINUTE, minute);
        monday = new SimpleDateFormat("yyyyMMddHHmmss").format(cal.getTime());
        return monday;
    }

    public static Integer dateFormatToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return Integer.parseInt(sdf.format(date));
    }

    public static Integer dateFormatToInteger(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
        return Integer.parseInt(sdf.format(date));
    }

    //Long时间戳返回格式化日期字符串 yyyy-MM-dd HH:mm
    public static String stampToDateString(Long stampLong) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date(stampLong);
        return sdf.format(date);
    }

    /**
     * 得到Long时间戳 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String stampToformatDate(Long stampLong, Object... pattern) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(stampLong);
        String formatDate = null;
        if (pattern != null && pattern.length > 0) {
            formatDate = DateFormatUtils.format(cal.getTime(), pattern[0].toString());
        } else {
            formatDate = DateFormatUtils.format(cal.getTime(), "yyyy-MM-dd");
        }
        return formatDate;
    }

    /**
     * 时间格式字符串转 Long时间戳
     *
     * @param dateStr 时间格式字符串
     * @param pattern pattern可以为："yyyy-MM-dd"
     * @return
     */
    public static Long formatDateTostamp(String dateStr, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date da = new Date();
        try {
            da = sdf.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
        return da.getTime();
    }

    //字符串时间戳返回格式化日期字符串 yyyy-MM-dd HH:mm
    public static String stampToDateString(String stampStr) {
        long stampLong = new Long(stampStr);
        return stampToDateString(stampLong);
    }

    /**
     * 时间比较
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int compareDate(Date d1, Date d2) {
        if (d1.getTime() > d2.getTime()) {
            return 1;
        } else if (d1.getTime() < d2.getTime()) {
            return -1;
        } else {//相等
            return 0;
        }
    }

    /**
     * 根据type返回想要的相差值
     * dateStar-dateEnd 相差 多少type
     *
     * @param dateStar
     * @param dateEnd
     * @param type     (0:天|1:小时|2:分钟|3:秒)
     * @return
     */
    public static long getDistanceTimes(Date dateStar, Date dateEnd, Integer type) {
        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        long nh = 1000 * 60 * 60;//一小时的毫秒数
        long nm = 1000 * 60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数
        //获得两个时间的毫秒时间差异
        Long diff = dateStar.getTime() - dateEnd.getTime();
        long day = diff / nd;//计算差多少天
        long hour = diff % nd / nh;//计算差多少小时
        long min = diff % nd % nh / nm;//计算差多少分钟
        long sec = diff % nd % nh % nm / ns;//计算差多少秒//输出结果
        long tem = 0;
        switch (type) {
            case 0:
                tem = day;
                break;
            case 1:
                tem = (day * 24) + hour;
                break;
            case 2:
                tem = (((day * 24) + hour) * 60) + min;
                break;
            case 3:
                tem = (((((day * 24) + hour) * 60) + min) * 60) + sec;
                break;
            default:
                break;
        }
        return tem;
    }

    /**
     * @param interval 间隔 0 当月 ，1上一个月...以此类推
     * @param type     类型 0 第一天 ，1最后一天
     * @param model    返回类型  0
     * @return 0:'yyyy-MM-dd' 形式的String  1:时间戳形式的String
     */
    public static String getMonthDay(int interval, int type, int model) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.add(Calendar.MONTH, -interval);

        if (type == 0) {
            calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        } else {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        }

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        if (model == 0) {
            return formatDate(new Date(calendar.getTimeInMillis()), "yyyy-MM-dd");
        } else {
            return calendar.getTimeInMillis() + "";
        }

    }

    public static String getYearDay(int type, int model) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期

        if (type == 0) {
            calendar.set(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        } else {
            calendar.set(Calendar.MONTH, 11);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        if (model == 0) {
            return formatDate(new Date(calendar.getTimeInMillis()), "yyyy-MM-dd");
        } else {
            return calendar.getTimeInMillis() + "";
        }
    }

    //获取当日0点的时间戳
    public static long getTimesmorning() {
        return getTimesmorning(System.currentTimeMillis());
    }

    //获得当天24点时间
    public static long getTimesnight() {
        return getTimesnight(System.currentTimeMillis());
    }

    //获得明天24点时间
    public static long getTimesTomorrownight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis() + 24 * 60 * 60 * 1000;
    }

    //获得本周一0点时间
    public static long getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTimeInMillis();
    }

    //获得本周六0点时间
    public static long getTimesWeekendmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        return cal.getTimeInMillis();
    }

    //获得本周日24点时间
    public static long getTimesWeeknight() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime().getTime() + (7 * 24 * 60 * 60 * 1000);
    }

    //获得本月第一天0点时间
    public static long getTimesMonthmorning() {
        return getTimesMonthmorning(System.currentTimeMillis());
    }

    //获得本月最后一天24点时间
    public static long getTimesMonthnight() {
        return getTimesMonthnight(System.currentTimeMillis());
    }

    /**
     * 获取指定时间0点的时间戳
     *
     * @param time
     * @return
     */
    public static long getTimesmorning(Long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     * 获取指定时间0点的时间戳
     *
     * @param day
     * @return
     */
    public static long getTimesmorning(int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH - day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }


    /**
     * 获得指定时间24点时间
     *
     * @param time
     * @return
     */
    public static long getTimesnight(Long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     * 获得指定day天前的0点时间
     *
     * @param day
     * @return
     */
    public static long getTimesMorningByDayBefore(Integer day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     * 获得指定时间月的第一天0点时间
     *
     * @param time
     * @return
     */
    public static long getTimesMonthmorning(Long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTimeInMillis();
    }

    //获取某年1-12月的初始时间戳 month 1-12 year为空为今年 year格式：2017
    public static long getTimesMonthmorning(Integer month, Integer... year) {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), month - 1, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        if (year != null && year.length > 0 && year[0] != null) {
            cal.set(year[0], month - 1, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        }
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    //获取某年1-12月的初始时间 month 1-12 year为空为今年 year格式：2017
    public static Date getDateMonthmorning(Integer month, Integer year) {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), month - 1, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        if (year != null) {
            cal.set(year, month - 1, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        }
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定时间月的最后一天24点时间
     *
     * @param time
     * @return
     */
    public static long getTimesMonthnight(Long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return cal.getTimeInMillis();
    }

    //获取某段时间内的所有日期
    public static List<Date> findDates(Date dBegin, Date dEnd) {
        List lDate = new ArrayList();
        lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }

    //获取某段时间段内的字符串日期list
    public static List<String> findStrDates(String startDate, String endDate) {
        List<String> dateList = new ArrayList<>();
        Date start = formatToDate(startDate);
        Date end = formatToDate(endDate);
        List<Date> dates = findDates(start, end);
        for (Date date : dates) {
            dateList.add(formatDate(date));
        }
        return dateList;
    }

    /**
     * 获取指定日期是星期几
     * 参数为null时表示获取当前日期是星期几
     *
     * @param date
     * @return 返回星期字符串
     */
    public static String getStrWeekOfDate(Date date) {
        String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekOfDays[w];
    }

    /**
     * 获取指定日期是星期几
     * 参数为null时表示获取当前日期是星期几
     *
     * @param date
     * @return
     */
    public static Integer getIntWeekOfDate(Date date) {
        Integer[] weekOfDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekOfDays[w];
    }

    /**
     * 通过生日获得年龄
     */
    public static Integer getAgeByBrithday(Long brithday) {
        Date date = new Date(brithday);
        //出生的年份
        Integer brithdayYear = Integer.valueOf(formatDateOnlyYear(date));
        //当前年份
        Integer nowYear = Integer.valueOf(getYear());
        Integer age = 0;
        if (nowYear - brithdayYear >= 0) {
            age = (nowYear - brithdayYear) + 1;
        }
        return age;
    }

    /**
     * 获取指定日期前hour小时的整点时间
     *
     * @param date 指定日期
     * @param hour 距离指定日期几小时
     * @return 指定日期前几小时的整点时间
     */
    public static long getSharpHourBefore(Date date, Integer hour) {

        Calendar calendar = Calendar.getInstance();// 获取当前日期
        if (date != null) {
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取指定日期之后hour小时的整点时间
     *
     * @param date 指定日期
     * @param hour 距离指定日期几小时
     * @return 指定日期之后几小时的整点时间
     */
    public static long getSharpHourAfter(Date date, Integer hour) {

        Calendar calendar = Calendar.getInstance();// 获取当前日期
        if (date != null) {
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取指定日期前hour小时的时间
     *
     * @param date 指定日期 null则取当前时间
     * @param hour 距离指定日期几小时
     * @return 指定日期前几小时的时间 例4:32前3小时 1:32
     */
    public static long getHourBefore(Date date, Integer hour) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        if (date == null) {
            date = new Date();
        }
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取指定日期后hour小时的时间
     *
     * @param date 指定日期 null则取当前时间
     * @param hour 距离指定日期几小时
     * @return 指定日期后几小时的时间 例4:32后1小时 5:32
     */
    public static long getHourAfter(Date date, Integer hour) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        if (date == null) {
            date = new Date();
        }
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
        return calendar.getTimeInMillis();
    }

    /**
     * @param args
     * @throws ParseException
     */
    public static void main(String[] args) throws ParseException {
//		System.out.println(formatDate(parseDate("2010/3/6")));
//		System.out.println(getDate("yyyy年MM月dd日 E"));
//		long time = new Date().getTime()-parseDate("2012-11-19").getTime();
        //System.out.println(getWeekBetween(-2));
        //System.out.println(getWeekBetween(-1));
        System.out.println(formatDate(getDateMonthmorning(1,2018),"yyyy-MM-dd HH:mm:ss"));
        System.out.println(formatDate(new Date(getTimesMonthmorning(1,2018)),"yyyy-MM-dd HH:mm:ss"));
        System.out.println(new Date(1353254400000L));
    }


}
